<template>
	<view>
		<wrapBgImgVue
			bgImg="goods/goods_top.png"
		/>
		
		<navigationBarVue
			:bgImgType="1"
			navigationTitle="分销订单"
			@onTotalHeight="onTotalHeight"
		/>
		
		<view class="wrap safe-area"
			:style="'padding-top: calc(' + (topHeight) + 'px) !important;'"
		>
			<view class="head-box">
				<image
					class="bg-img"
					mode="widthFix"
					:src="common.appUrl.oss + common.imgList.tgbg"
				/>
				
				<view class="down-con">
					<view class="commission-con">
						<view class="name">推广订单</view>
						<view class="price">{{ totalOrderNum }}</view>
					</view>
					
					<view class="commission-con">
						<view class="name">推广订单金额</view>
						<view class="price">{{ totalOrderMoney }}</view>
					</view>
				</view>
			</view>
			
			<view class="title-con">
				<view class="time-box" @click="onCalendar">
					<view class="time">
						{{ starTime == endTime ? starTime : `${starTime}至${endTime}` }}
					</view>
					
					<view class="wlIcon-sanjiao1 icons"></view>
				</view>
				
				<view class="all">全部收入￥{{ totalMoney }}</view>
			</view>
			
			<view class="tab-box">
				<block v-for="(item, index) in tab_list" :key="index">
					<view class="tab-more" @click="onTab(item)">
						<view :class="status === item.type ? 'tab-name select-color' : 'tab-name'">
							{{ item.title }}
						</view>
							
						<view :class="status === item.type ? 'tab-line opacity' : 'tab-line'"></view>
					</view>
				</block>
			</view>
			
			<view>
				<block v-for="(item, index) in list" :key="index">
					<view class="order-list">
						<view class="order-head">
							<view class="order-number">
								订单编号：{{ item.orders.order_no }}
							</view>
							
							<view class="order-state">
								已完成
							</view>
						</view>
						
						<block v-for="(goodsItem, idx) in item.goods" :key="index">
							<view class="order-detail">
								<image
									:src="$wanlshop.oss(goodsItem.image)"
									mode="aspectFill"
								/>
									
								<view class="order-con">
									<view class="order-con-detail">
										<view class="order-name">
											{{ goodsItem.title }}
										</view>
										
										<view class="order-num">
											x{{ goodsItem.number }}
										</view>
									</view>
									
									<view class="order-con-box">
										<view class="order-money">
											￥{{ goodsItem.parent_draw }}
										</view>
										
										<view class="order-type">
											{{ ["", "待入账", "已入账", "已退回", "已取消"][item.state] }}
										</view>
									</view>
								</view>
							</view>
						</block>
						
						<view class="order-box">
							<view class="order-user">
								<image
									:src="$wanlshop.oss(item.source.avatar)"
									mode="aspectFill"	
								/>
								<view>
									{{ item.source.nickname }}
								</view>
							</view>
							
							<view class="order-time">
								{{ item.createtime_test }}
							</view>
						</view>
					</view>
				</block>
			</view>
		</view>

		<wu-calendar
			ref="calendar"
			color="#84BD00"
			mode="range"
			@confirm="calendarConfirm"
			:insert="false"
		/>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	
	import { GetCommissionList } from "../API/team.js";
	import navigationBarVue from "../components/navigationBar/navigationBar.vue";
	import wrapBgImgVue from "../components/navigationBar/bgImg.vue";
	
	export default {
		components: {
			wrapBgImgVue,
			navigationBarVue,
		},
		computed: {
			...mapState(['common'])
		},
		data() {
			return {
				topHeight: 0, //顶部高度
				tab_list: [
					{ title: "全部", type: "0" },
					{ title: "待入账", type: "1" },
					{ title: "已计入", type: "2" },
					{ title: "已退回", type: "3" },
					// { title: "已取消", type: "4" },
				], //切换
				status: "0", //
				
				starTime: '',
				endTime: '',
				
				totalOrderNum: "0", //订单总数
				totalOrderMoney: "0.00", //订单总价格
				totalMoney: "0.00", //总价格
				page: 1,
				total: 0,
				list: [],
			}
		},
		onLoad() {
			const now = new Date();
			const year = now.getFullYear();
			const month = String(now.getMonth() + 1).padStart(2, '0');
			const day = String(now.getDate()).padStart(2, '0');
			const formattedDate = `${year}-${month}-${day}`;
			this.starTime = formattedDate
			this.endTime = formattedDate;
			
			this.onRefresh();
		},
		onPullDownRefresh() {
			this.onRefresh();
			this.getData();
		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++;
				this.getListData();
			}
		},
		methods: {
			/**
			 * 顶部高度
			 */
			onTotalHeight(height) {
				this.topHeight = height;
			},
			/**
			 * 点击切换
			 */
			onTab(item) {
				if (this.status === item.type) {
					return;
				}
				this.status = item.type;
				this.onRefresh();
			},
			/**
			 * 选择日历
			 */
			onCalendar() {
				this.$refs.calendar.open();
			},
			//确定选择
			calendarConfirm(e) {
				this.starTime = e.range.before
				this.endTime = e.range.after
				
				this.onRefresh();
			},
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1;
				this.list = [];
				this.getListData();
			},
			/**
			 * 获取列表
			 */
			getListData() {
				var date = `${this.starTime} - ${this.endTime}`;
				GetCommissionList({
					page: this.page,
					state: this.status,
					time:  date,
				}).then(res => {
					this.total = res.data.list.total;
					const list = res.data.list.data;
					this.totalMoney = res.data.total_money;
					this.totalOrderMoney = res.data.total_order_money;
					this.totalOrderNum = res.data.total_order_num;
					if (this.page == 1) {
						this.list = list;
					} else {
						this.list = this.list.concat(list);
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.head-box {
		width: 690rpx;
		position: relative;
		background-color: #84BD00;
		margin-left: 30rpx;
		margin-top: 30rpx;
		border-radius: 20rpx;

		.bg-img {
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			right: 0;
		}

		.up-con {
			width: 610rpx;
			margin-left: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 50rpx 0;
			border-bottom: 1px solid rgba(255, 255, 255, 0.2);
			position: relative;

			.income-con {
				.title {
					font-size: 26rpx;
					color: #fff;
				}

				.money {
					font-size: 36rpx;
					color: #fff;
					margin-top: 10rpx;
				}
			}

			.withdrawal {
				font-size: 24rpx;
				color: #84BD00;
				width: 112rpx;
				height: 52rpx;
				border-radius: 50rpx;
				background: #FFFFFF;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.down-con {
			width: 610rpx;
			margin-left: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx 0;
			position: relative;

			.commission-con {
				width: 300rpx;

				.name {
					font-size: 26rpx;
					color: #fff;
				}

				.price {
					font-size: 36rpx;
					color: #FFFFFF;
					margin-top: 10rpx;
				}
			}
		}
	}

	.title-con {
		width: 690rpx;
		margin-left: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 0;
		position: relative;

		.time-box {
			display: flex;
			align-items: center;
			background-color: #FFFFFF;
			padding: 10rpx 26rpx;
			border-radius: 50rpx;

			.time {
				font-size: 30rpx;
				color: #3D3D3D;
			}

			.icons {
				font-size: 24rpx;
				color: #999999;
				margin-left: 10rpx;
			}
		}

		.all {
			font-size: 30rpx;
			color: #3D3D3D;
		}
	}

	.tab-box {
		width: 750rpx;
		display: flex;
		justify-content: center;
		margin-bottom: 30rpx;
		position: relative;

		.tab-more {
			width: 25%;
			display: flex;
			flex-wrap: wrap;
			justify-content: center;

			.tab-name {
				width: 100%;
				text-align: center;
				font-size: 30rpx;
				color: #333333;
			}

			.select-color {
				color: #84BD00;
				font-weight: bold;
			}

			.tab-line {
				width: 40rpx;
				height: 6rpx;
				background-color: #84BD00;
				border-radius: 50rpx;
				margin-top: 6rpx;
				opacity: 0;
			}

			.opacity {
				opacity: 1;
			}
		}
	}

	.order-list {
		width: 690rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-left: 40rpx;
		margin-top: 20rpx;
		padding-bottom: 30rpx;

		.order-head {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx 0;

			.order-number {
				font-size: 26rpx;
				color: #999999;
			}

			.order-state {
				font-size: 26rpx;
				color: #84BD00;
			}
		}

		.order-detail {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			image {
				width: 120rpx;
				height: 120rpx;
				border-radius: 16rpx;
			}

			.order-con {
				width: 490rpx;
				height: 110rpx;
				display: flex;
				flex-wrap: wrap;
				align-content: space-between;

				.order-con-detail {
					width: 100%;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.order-name {
						font-size: 26rpx;
						color: #333333;
					}

					.order-num {
						font-size: 30rpx;
						color: #999999;
					}
				}

				.order-con-box {
					width: 100%;
					display: flex;
					align-items: center;

					.order-money {
						font-size: 34rpx;
						font-weight: bold;
						color: #333333;
					}

					.order-type {
						margin-left: 10rpx;
						width: 108rpx;
						height: 36rpx;
						border-radius: 50rpx;
						font-size: 24rpx;
						color: #84BD00;
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: rgba(255, 102, 0, 0.1);
					}
				}
			}
		}

		.order-box {
			width: 630rpx;
			height: 80rpx;
			border-radius: 20rpx;
			margin-left: 30rpx;
			background: #F6F6F6;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 40rpx;

			.order-user {
				display: flex;
				align-items: center;
				margin-left: 20rpx;

				image {
					width: 48rpx;
					height: 48rpx;
					border-radius: 50%;
				}

				view {
					margin-left: 18rpx;
					font-size: 26rpx;
					color: #3D3D3D;
				}
			}

			.order-time {
				margin-right: 20rpx;
				font-size: 26rpx;
				color: #999;
			}
		}
	}
</style>